package com.JFrameView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * author:hlc
 * date:2023/9/24
 */

/**
 * 实现鼠标拖动窗口
 * @author 17930
 */
public class WindowController extends MouseAdapter implements MouseMotionListener {
   private Point point;
   private JFrame jFrame;

   public WindowController(JFrame jFrame) {
      point = new Point();
      this.jFrame = jFrame;
   }

   @Override
   public void mouseDragged(MouseEvent e) {
      Point location = jFrame.getLocation();
      jFrame.setLocation(location.x + e.getX() - point.x, location.y + e.getY() - point.y);
   }

   /**
    * 鼠标按下
    *
    * @param e the event to be processed
    */
   @Override
   public void mousePressed(MouseEvent e) {
      point.x = e.getX();
      point.y = e.getY();
   }
}
